package org.example.car.system.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import org.example.car.system.domain.SysMenu;
import org.example.car.system.mapper.SysMenuMapper;
import org.example.car.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class SysMenuimplService implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;




    @Override
    public List<SysMenu> listQuery (SysMenu entity) {
       List<SysMenu> menuList = sysMenuMapper.selectMenu(entity);
        return menuList;
    }

    @Override
    public void insertbyquery(SysMenu entity)throws Exception {
        sysMenuMapper.insertentity(entity);
    }



    @Override
    public void deleteByQuery(Integer menuId) throws Exception {
       List<SysMenu> list = sysMenuMapper.selectbyId(menuId);
       if (list!=null){
           System.err.println(list);
           //通过java类 new出一个stringbuilder对象进行拼接
           StringBuilder ids = new StringBuilder();
           for (SysMenu sysMenu : list) {
               ids.append(sysMenu.getMenuId());
               List<SysMenu> list1 = sysMenuMapper.selectbyId(sysMenu.getMenuId());
               if (list1!=null){
                   StringBuilder ids1 = new StringBuilder();
                   for (SysMenu sysMenu1 : list1) {
                       ids1.append(sysMenu1.getMenuId());
                   }
                   if (ids1!=null){
                       sysMenuMapper.deleteByParentId(ids1.toString());
                   }
               }
           }
           if (ids!=null){
               sysMenuMapper.deleteByParentId(ids.toString());
           }
       }
        sysMenuMapper.deleteNormal(menuId);
    }

    @Override
    public SysMenu sentimentality(Integer parentId) {
        SysMenu sysMenus = sysMenuMapper.sentimentality(parentId);
        return sysMenus;
    }

    @Override
    public void updateForm(SysMenu entity)throws Exception {
         sysMenuMapper.updatemysql(entity);
    }

    @Override
    public List<Tree<String>> selecaddtee() {
        List<SysMenu> selectall = sysMenuMapper.selectall();
                //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("value");
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setNameKey("label");
        treeNodeConfig.setParentIdKey("parentId");
// 最大递归深度
//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(selectall, "0", treeNodeConfig,
                (sysMenu, tree) -> {
                    tree.setId(sysMenu.getMenuId()+"");
                    tree.setParentId(sysMenu.getParentId()+"");
                    tree.setName(sysMenu.getMenuName());
                });
        return treeNodes;
    }
}
